package com.prjsystem.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.prjsystem.entity.UserInfo;


public class FilterAction implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
	@SuppressWarnings("unchecked")
	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		 HttpServletRequest request = (HttpServletRequest) req; 
		 HttpServletResponse response = (HttpServletResponse) res; 
		 HttpSession session = request.getSession();      
		 String path=request.getRequestURI();
		 List<UserInfo> userInfo=(List<UserInfo>)session.getAttribute("privileges");
		if(path.equals("/struts22/init_login.action")||path.equals("/struts22/login_login.action"))
	    {
			chain.doFilter(request,response);
		}
		else
		{
		      try{
				    for(int i=0;i<userInfo.size();i++)
				 {
				   if(path.equals(userInfo.get(i).getFuncurl()))
				   {
					 chain.doFilter(request,response);
				   }
				 }
		      }
		     catch(Exception e)
		     {
		    	 e.printStackTrace();

		     }
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
